package com.bootdo.hjh.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bootdo.common.annotation.Log;
import com.bootdo.common.utils.R;
import com.bootdo.hjh.cache.MemberCache;
import com.bootdo.hjh.cache.NodeInfo;
import com.bootdo.hjh.domain.BankCard;
import com.bootdo.hjh.service.BankCardService;
import com.bootdo.system.domain.UserDO;
import com.google.common.collect.Maps;

/**
 * Created by qingyong on 2018/3/31.
 */
@Controller
public class BankCardController {

	@Autowired
	private BankCardService bankCardService;
	
	@Autowired
	private MemberCache memberCache;


    @Log("银行卡列表-页面跳转")
    @GetMapping("/bankcard_list")
    public String bankCardList(Model model){
        //获取当前用户
        Subject subject = SecurityUtils.getSubject();
        Map<String,Object> recomment = Maps.newHashMap();
        Object obj = subject.getPrincipal();
        Long userId = ((UserDO)obj).getUserId();
        List<BankCard> list = bankCardService.list(userId);
        Map<String,Object> metadata=new HashMap<>();
        model.addAttribute("list",list);
        model.addAttribute("total",list.size());
        return "hjh/bankcard";
    }
    
    @Log("银行卡列表-获取银行卡信息")
    @GetMapping("/bankcard_get")
    @ResponseBody
    public BankCard bankCardGet(Model model,Integer id){
        //获取当前用户
        Subject subject = SecurityUtils.getSubject();
        Map<String,Object> recomment = Maps.newHashMap();
        Object obj = subject.getPrincipal();
        Long userId = ((UserDO)obj).getUserId();
        return bankCardService.get(id);
    }
    
    @Log("银行卡管理-添加")
    @PostMapping("/bankcard_add")
    @ResponseBody
    public R bankCardAdd(Model model,BankCard bankCard){
        //获取当前用户
        Subject subject = SecurityUtils.getSubject();
        Map<String,Object> recomment = Maps.newHashMap();
        Object obj = subject.getPrincipal();
        Long userId = ((UserDO)obj).getUserId();
        NodeInfo userInfo = memberCache.getNodeInfo(userId) ;
        bankCard.setUserId(userId);
        if(!userInfo.getName().equals(bankCard.getCardUserName().trim())) {
        		return R.error(101, "银行卡所有人与会员真实姓名不统一");
        }
        if(bankCard.getId() != null) {
        		bankCardService.updateBankCard(bankCard);
        }else {
        		bankCardService.addBankCard(bankCard);
        }
        return new R();
    }
    
    @Log("银行卡管理-删除")
    @GetMapping("/bankcard_delete")
    @ResponseBody
    public R cashTransfer(Model model,Integer id){
        //获取当前用户
        Subject subject = SecurityUtils.getSubject();
        Map<String,Object> recomment = Maps.newHashMap();
        Object obj = subject.getPrincipal();
        Long userId = ((UserDO)obj).getUserId();
        bankCardService.deleteBankCard(id);
        return new R();
    }
    
    
}
